package controller

import (
	"gin/common"
	"gin/model"
	"gin/response"
	"gin/util"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"strconv"
	"time"
)

func GetCouponsList(ctx *gin.Context) {
	Db := common.GetDB()
	//user, _ := ctx.Get("user")
	var CouponsList []model.CouponsInfo
	shopId := ctx.PostForm("shopId")
	Db.Debug().Table("cat_meal_coupons").
		Where("shopId=?", shopId).Order("id desc").Find(&CouponsList)
	response.Success(ctx, gin.H{"list": CouponsList}, "SUCCESS")
}
func GetMyCouponsList(ctx *gin.Context) {
	Db := common.GetDB()
	user, _ := ctx.Get("user")
	status := ctx.PostForm("status")
	if len(status) == 0 {
		status = ""
	} else {
		status = "status = " + status
	}
	var CouponsList []model.MyCouponsInfo
	shopId := ctx.PostForm("shopId")
	Db.Debug().Table("cat_meal_user_coupons").
		Where("shopId=? AND uid=?", shopId, user.(model.UserInfo).Uid).Where(status).Order("id desc").Find(&CouponsList)
	for _, v := range CouponsList {
		if v.DateEnd < util.DateFormat(0) && v.Status == 0 {
			Db.Debug().Table("cat_meal_user_coupons").Where("shopId=? AND uid=?", shopId, user.(model.UserInfo).Uid).Update("status", 1)
		}
	}
	response.Success(ctx, gin.H{"list": CouponsList}, "SUCCESS")
}

func ReceiveCoupons(ctx *gin.Context) {
	Db := common.GetDB()
	user, _ := ctx.Get("user")
	var CouponsInfo model.CouponsInfo
	Id := ctx.PostForm("couponsId")
	Db.Debug().Table("cat_meal_coupons").Where("id=?", Id).Order("id desc").First(&CouponsInfo)
	if CouponsInfo.Id == 0 {
		response.Fail(ctx, gin.H{}, "记录不存在")
		return
	}
	if CouponsInfo.Status == 0 {
		response.Fail(ctx, gin.H{}, "下架了")
		return
	}
	if CouponsInfo.NumberTotle < 1 {
		response.Fail(ctx, gin.H{}, "已经被领完了")
		return
	}
	var myCou model.MyCouponsInfo
	Db.Debug().Table("cat_meal_user_coupons").Where("coupons_id=? AND uid=?", Id, user.(model.UserInfo).Uid).First(&myCou)
	if myCou.Id > 0 {
		response.Fail(ctx, gin.H{}, "你已经领取过了")
		return
	}

	if CouponsInfo.NeedScore > 0 {
		var userI model.UserInfo
		Db.Debug().Table("cat_meal_user").Where("uid=?", user.(model.UserInfo).Uid).First(&userI)
		if userI.Score < CouponsInfo.NeedScore {
			response.Fail(ctx, gin.H{}, "积分不够")
			return
		}
	}
	CouponsId, _ := strconv.Atoi(Id)
	var insert = model.MyCouponsInfo{
		DateAdd:       util.DateFormat(0),
		Name:          CouponsInfo.Name,
		Money:         CouponsInfo.MoneyMin,
		MoneyHreshold: CouponsInfo.MoneyHreshold,
		Uid:           user.(model.UserInfo).Uid,
		DateEnd:       util.DateFormat(time.Now().Unix() + CouponsInfo.DateEndDays*24*60*60),
		Status:        0,
		CouponsId:     CouponsId,
		ShopId:        CouponsInfo.ShopId,
	}
	err := Db.Transaction(func(tx *gorm.DB) error {
		if err := Db.Debug().Table("cat_meal_user_coupons").Create(&insert).Error; err != nil {
			// 返回任何错误都会回滚事务
			tx.Rollback()
			return err
		}
		//修改优惠券状态
		if err := Db.Debug().Table("cat_meal_coupons").Where(" id=? ", CouponsId).Update("numberTotle", CouponsInfo.NumberTotle-1).Error; err != nil {
			tx.Rollback()
			return err
		}
		err := model.ChangeScore(Db, user.(model.UserInfo), -CouponsInfo.NeedScore, 2)
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return
	}

	response.Success(ctx, gin.H{}, "成功")

}
